package com.lancoo.ccas52.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas52.aspect.LogAnnotation;
import com.lancoo.ccas52.entity.Room;
import com.lancoo.ccas52.pojo.common.PageInfo;
import com.lancoo.ccas52.pojo.vo.ClassTeachingClassDto;
import com.lancoo.ccas52.pojo.vo.TeacherStatistics;
import com.lancoo.ccas52.pojo.vo.TeachingClassCourseDto;
import com.lancoo.ccas52.pojo.vo.TeachingClassScheduleDto;
import com.lancoo.ccas52.service.RoomService;
import com.lancoo.ccas52.service.TimetableService;
import com.lancoo.ccas52.util.ExcelFormatUtil;
import com.lancoo.ccas52.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "课表")
@RequestMapping("/api/v5.2.1/pc/schedule")
public class TimetableController {


    @Resource
    private TimetableService timetableServiceImpl;

    @Resource
    private RoomService roomService;

    @ApiOperation("获取全校课表")
    @GetMapping(value = "/course")
    public ResponseObject<PageInfo<TeachingClassCourseDto>> getSchoolSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                              @ApiParam(value = "课程类别id 传null全部", required = false) @RequestParam(required = false) String courseTypeId,
                                                                              @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                                              @ApiParam(value = "课程性质 1必修  2选修", required = false) @RequestParam(required = false) Integer courseNature,
                                                                              @ApiParam(value = "专业性质，1:公共课，2:专业课", required = false) @RequestParam(required = false) Integer majorProp,
                                                                              @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                              @ApiParam(value = "课程模糊查询 ", required = false) @RequestParam(required = false) String keyWord,
                                                                              @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                              @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassCourseDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(timetableServiceImpl.getSchoolSchedule(page, taskId, courseTypeId, studyNature, courseNature, majorProp, collegeId, keyWord));
    }

    @ApiOperation("统计教师教学信息")
    @GetMapping(value = "/teacher/statistics")
    public ResponseObject<PageInfo<TeacherStatistics>> getTeacherStatistics(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                            @ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                                            @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                                            @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                                            @ApiParam(value = "课程模糊查询 ", required = false) @RequestParam(required = false) String keyWord,
                                                                            @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                            @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<TeachingClassCourseDto> page = new Page<>(currentPage, pageSize);
        page.setOptimizeCountSql(false);
        return ResponseObject.ofSuccess(timetableServiceImpl.getTeacherStatistics(taskId, schoolId, term, currentPage, pageSize, collegeId, keyWord));
    }

    @ApiOperation("获取教师课表")
    @GetMapping(value = "/teacher")
    public ResponseObject<List<TeachingClassScheduleDto>> getTeacherSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                             @ApiParam(value = "老师id", required = true) @RequestParam(required = false) Long teacherId) {
        return ResponseObject.ofSuccess(timetableServiceImpl.getTeacherSchedule(taskId, teacherId));
    }

    @ApiOperation("获取教室列表")
    @GetMapping(value = "/roomList")
    public ResponseObject<List<Room>> getRoomSimpleList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                        @ApiParam(value = "学校Id", required = true) @RequestParam String schoolId,
                                                        @ApiParam(value = "学期", required = true) @RequestParam String term,
                                                        @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                        @ApiParam(value = "楼层", required = false) @RequestParam(required = false) Integer buildingFloor,
                                                        @ApiParam(value = "校区id", required = false) @RequestParam(required = false) String campusId,
                                                        @ApiParam(value = "教室类型", required = false) @RequestParam(required = false) Integer roomType,
                                                        @ApiParam(value = "教室名、楼宇名模糊查询", required = false) @RequestParam(required = false) String keyword,
                                                        @ApiParam(value = "0为不可用 1为可用", required = false) @RequestParam(required = false) Integer isEnable,
                                                        @ApiParam(value = "指定院系场地范围", required = false) @RequestParam(required = false) String collegeId) {
        return ResponseObject.ofSuccess(roomService.getRoomSimpleList(schoolId, term, buildingId, buildingFloor, keyword, campusId, roomType, taskId,isEnable, collegeId));
    }


    @ApiOperation("获取教室课表")
    @GetMapping(value = "/room")
    public ResponseObject<List<TeachingClassCourseDto>> getRoomSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                        @ApiParam(value = "教室id", required = true) @RequestParam(required = false) Long roomId) {
        return ResponseObject.ofSuccess(timetableServiceImpl.getRoomSchedule(taskId, roomId));
    }

    @ApiOperation("获取行政班列表")
    @GetMapping(value = "/classList")
    public ResponseObject<PageInfo<ClassTeachingClassDto>> getClassList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                        @ApiParam(value = "学院id", required = false) @RequestParam(required = false) String collegeId,
                                                                        @ApiParam(value = "行政班名模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                                        @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                        @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(timetableServiceImpl.getClassList(taskId, currentPage, pageSize, collegeId, keyWord));
    }

    @ApiOperation("获取学生课表")
    @GetMapping(value = "/student")
    public ResponseObject<List<TeachingClassCourseDto>> getStudentSchedule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                           @ApiParam(value = "学生id", required = true) @RequestParam(required = false) Long studentId) {
        return ResponseObject.ofSuccess(timetableServiceImpl.getStudentSchedule(taskId, studentId));
    }


    @ApiOperation("导出全校课表")
    @GetMapping(value = "/excel/course")
    @LogAnnotation(operationType = "14", operationContent = "导出全校课表")
    public ResponseObject<Object> schoolScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                        @ApiParam(value = "课程类别id 传null全部", required = false) @RequestParam(required = false) String courseTypeId,
                                                        @ApiParam(value = "课程类别名字", required = false) @RequestParam(required = false) String courseType,
                                                        @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                        @ApiParam(value = "课程性质 1必修  2选修", required = false) @RequestParam(required = false) Integer courseNature,
                                                        @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                        @ApiParam(value = "开课单位名字 ", required = false) @RequestParam(required = false) String collegeName) {
        return ResponseObject.ofSuccess(timetableServiceImpl.schoolScheduleToExcel(taskId, courseTypeId, courseType, studyNature, courseNature, collegeId, collegeName));
    }

    @ApiOperation("导出环节全校课表")
    @GetMapping(value = "/excel/segment")
    @LogAnnotation(operationType = "14", operationContent = "导出环节全校课表")
    public ResponseObject<Object> schoolSegmentScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                               @ApiParam(value = "环节类别id 传null全部", required = false) @RequestParam(required = false) String courseTypeId,
                                                               @ApiParam(value = "环节类别名字", required = false) @RequestParam(required = false) String courseType,
                                                               @ApiParam(value = "修读性质 1主 2辅修", required = false) @RequestParam(required = false) Integer studyNature,
                                                               @ApiParam(value = "学院id ", required = false) @RequestParam(required = false) String collegeId,
                                                               @ApiParam(value = "开课单位名字 ", required = false) @RequestParam(required = false) String collegeName) {
        return ResponseObject.ofSuccess(timetableServiceImpl.schoolSegmentScheduleToExcel(taskId, courseTypeId, courseType, studyNature, collegeId, collegeName));
    }

    @ApiOperation("导出教师课表")
    @GetMapping(value = "/excel/teacher")
    @LogAnnotation(operationType = "14", operationContent = "导出教师课表")
    public ResponseObject<Object> teacherScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "老师id", required = true) @RequestParam(required = false) Long teacherId) {
        return ResponseObject.ofSuccess(timetableServiceImpl.teacherScheduleToExcel(taskId, teacherId));
    }

    @ApiOperation("导出教室课表")
    @GetMapping(value = "/excel/room")
    @LogAnnotation(operationType = "14", operationContent = "导出教室课表")
    public ResponseObject<Object> roomScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "教室id", required = true) @RequestParam(required = false) Long roomId) {
        return ResponseObject.ofSuccess(timetableServiceImpl.roomScheduleToExcel(taskId, roomId));
    }

    @ApiOperation("导出学生课表")
    @GetMapping(value = "/excel/student")
    @LogAnnotation(operationType = "14", operationContent = "导出学生课表")
    public ResponseObject<Object> studentScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "学生id", required = true) @RequestParam(required = true) Long studentId) {
        return ResponseObject.ofSuccess(timetableServiceImpl.studentScheduleToExcel(taskId, studentId));
    }

    @ApiOperation("导出行政班课表")
    @GetMapping(value = "/excel/class")
    public ResponseObject<Object> classScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                       @ApiParam(value = "行政班id", required = false) @RequestParam(required = false) String classId,
                                                       HttpServletRequest request,
                                                       HttpServletResponse response) {
        ServletOutputStream out = ExcelFormatUtil.pretreatment(request, response, "class.xlsx");
        timetableServiceImpl.classScheduleToExcel(taskId, out, classId);
        return ResponseObject.ofSuccess();
    }

    @ApiOperation("导出班级、教室、教师总表")
    @GetMapping(value = "/excel/classTotal")
    public ResponseObject<Object> summaryScheduleToExcel(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "1:行政班 2:教师 3:教室", required = true) @RequestParam(required = true) Integer scheduleType,
                                                         HttpServletRequest request,
                                                         HttpServletResponse response) {
        ServletOutputStream out = ExcelFormatUtil.pretreatment(request, response, "summarySchedule.xlsx");
        timetableServiceImpl.summaryScheduleToExcel(taskId, out, scheduleType);
        return ResponseObject.ofSuccess();
    }


}
